package demo.test.controller;


import com.alibaba.fastjson.JSON;
import com.entites.DataDeviceSet;
import com.entites.DeviceSet;
import com.entites.PlamSet;
import demo.test.service.DataTransForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class TransFormController {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private DataTransForm dataTransForm;



    //要素数据收集系统在服务注册中心中的服务名称
    @Value("${service-url.nacos-user-service}")
private  String CollectionURL;
    @Value("${service-url.test-url}")
private String PlamURL;

    @GetMapping("/TransFormer/{id}")
    public DeviceSet setPara(@PathVariable("id") Integer id){

        DeviceSet device= restTemplate.getForObject(CollectionURL+"/get/"+id,DeviceSet.class);
        return device;

    }

    @GetMapping("/TransFormerTEST/{id}")
    public DeviceSet setParaTest(@PathVariable("id") Integer id){

        DeviceSet device= restTemplate.getForObject(CollectionURL+"/get/"+id,DeviceSet.class);
        return restTemplate.postForObject(CollectionURL+"/getO",device,DeviceSet.class);
    }


    @GetMapping("/TransFormers")
        public List<DeviceSet> getTransFormers(){
        List<DeviceSet>devices =restTemplate.getForObject(PlamURL, ArrayList.class);
        return devices;
        }


        @GetMapping("/Trans_Plam")
    public List<PlamSet> getTransPlam(){
List<PlamSet> plamSets=restTemplate.getForObject(PlamURL+"/Plam/getA",ArrayList.class);

return plamSets;
        }



        @GetMapping("/Trans_t_data_device")
    public List<Object> get_t_Data_device(){
            class NoName {
                public String name;
                public Integer value;
                public void set(String a,String b){
                    this.name=a;
                    this.value=Integer.valueOf(b);
                }
            }
        String json=restTemplate.getForObject(PlamURL+"/get/dataV-t_data_device",String.class);

            List<DataDeviceSet> result= JSON.parseArray(json,DataDeviceSet.class);
            List<Object> temp=new ArrayList<>();
            log.info(String.valueOf(result));
        for (DataDeviceSet i : result){
            NoName tempp= new NoName();
            tempp.set(i.getCDevicename(),i.getCDeviceid());
            temp.add(tempp);

        }
        return temp;
        }



}


//        @PatchMapping(path="/testPatch/{id}",consumes = "application/json")
//
//    public DeviceSet PatchTEst(@PathVariable("id") Integer id,@RequestBody DeviceSet device){
//
//
//
//
//
//        }






